package com.aaa.power.util;

import com.aaa.power.entity.OperLog;
import com.aaa.power.entity.User;
import com.aaa.power.service.OperLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @Author: YuXin
 * @Date : Created in 2020/11/12 14:06
 * @ClassName : OperationLogUtil
 * @Description : 通用操作日志工具
 * @Package_Name : com.aaa.power.util
 */
@Component
@Aspect  //标志为切面实现类
public class OperationLogUtil {

    /**
     * 依赖注入服务类
     */
    @Autowired
    private OperLogService operLogService;
    /**
     * 依赖注入 session
     */
    @Autowired
    private HttpSession session;

    /**
     * 记录操作日志
     *
     * @param joinPoint 获取连接点信息的类
     */
    @AfterReturning(pointcut = "execution(* com.aaa.power.service.*.insert*(..))||execution(* com.aaa.power.service.*.update*(..))||execution(* com.aaa.power.service.*.delete*(..))")
    public void recordOperationLog(JoinPoint joinPoint) {
        //获取执行业务的方法名称
        String methodName = joinPoint.getSignature().getName();
        int businessType = 0;
        if (methodName.startsWith("insert")) {
            businessType = 1;
        } else if (methodName.startsWith("update")) {
            businessType = 2;
        } else if (methodName.startsWith("delete")) {
            businessType = 3;
        }
        //获取方法全名称
        String className = joinPoint.getTarget().getClass().getName();
        //获取用户session信息
        Object userInfo = session.getAttribute("userInfo");
        User user = userInfo == null ? null : (User) userInfo;
        //获取request对象
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        //获取ip地址
        String remoteAddr = request.getRemoteAddr();
        //获取请求地址
        String requestURI = request.getRequestURI();
        //获取连接点所有参数
        Object[] args = joinPoint.getArgs();

        OperLog operLog = new OperLog();
        operLog.setTitle("权限管理");
        operLog.setBusinessType(businessType);
        operLog.setMethod(className + "." + methodName + "()");
        operLog.setOperatorType(1);
        if (user != null) {
            operLog.setOperName(user.getLoginName());
            operLog.setDeptName(user.getDeptId() + "");
        }
        operLog.setOperIp(remoteAddr);
        operLog.setOperUrl(requestURI);
        if (remoteAddr.startsWith("192") || remoteAddr.startsWith("::") || remoteAddr.startsWith("127") || remoteAddr.startsWith("0:0:0:")) {
            operLog.setOperLocation("内网IP");
        } else {
            operLog.setOperLocation("外网IP");
        }
        if (args != null && args.length > 0) {
            String simpleName = "";
            for (Object arg : args) {
                simpleName += arg.getClass().getSimpleName() + ",";
            }
            operLog.setOperParam(simpleName);
        }
        operLog.setOperTime(new Date());
        operLogService.add(operLog);

    }

}
